package com.system.healthproject.model.admin.service.impl;

import com.system.healthproject.bean.PagesBean;
import com.system.healthproject.mapper.LoginMapper;
import com.system.healthproject.model.admin.paramsbean.LoginParams;
import com.system.healthproject.model.admin.service.AdminUserService;
import com.system.healthproject.pojo.Login;
import com.system.healthproject.pojo.Menu;
import com.system.healthproject.pojo.Role;
import com.system.healthproject.service.RoleService;
import com.system.healthproject.utils.JwtUtil;
import com.system.healthproject.utils.Md5Utils;
import com.system.healthproject.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AdminUserServiceImp implements AdminUserService {

    @Autowired
    LoginMapper loginMapper;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    RoleService roleService;



    @Override
    public Boolean addUserRole(Integer userId, Integer roleId) {
        return loginMapper.addUserRole(userId,roleId)==1;
    }


    @Override
    public HashMap<String, Object> login(LoginParams loginParams) {
        loginParams.setPassword(Md5Utils.md5(loginParams.getPassword()));
        Map<String, Object> map = loginMapper.selectUserInfoByLoginParams(loginParams);
        if(map==null){
            return null;
        }
        if(map.containsKey("login_doctorId")||map.containsKey("login_patientId")){
            return null;
        }
        Integer loginState = (Integer) map.get("loginState");
        if(loginState==2){
            return null;
        }
        if(map.size()!=0){
            Map<String, Object> hashMap = new HashMap<>();
            Integer login_id = (Integer) map.get("loginId");
            String phone = (String) map.get("loginPhone");
            String tokenKey = phone + login_id;
            String oldToken = (String) redisUtil.get(tokenKey);
            if(oldToken!=null){
                //删除原本的信息缓存
                redisUtil.del(oldToken);
            }
            List<Role> roles = (List<Role>) map.get("roles");
            if(roles!=null&&roles.size()!=0){
                Role role = roles.get(0);
                Integer roleId = role.getRoleId();
                List<Menu> menuByRole = roleService.getMenuByRole(roleId);
                if(menuByRole!=null&&menuByRole.size()!=0){
                    Menu menu = menuByRole.get(0);
                    if(menu.getMenuName().equals("powerManager")){
                        menuByRole.add(menu);
                        menuByRole.remove(0);

                    }
                    for (int i=0;i<menuByRole.size();i++){
                        Menu menus = menuByRole.get(i);
                        if(menus.getMenuName().equals("patientManager")){
                            List<Menu> menuss = menus.getMenus();
                            Collections.reverse(menuss);
                            menus.setMenus(menuss);
                        }
                    }
                    role.setMenus(menuByRole);

                }

            }
            hashMap.put("userId", login_id);
            hashMap.put("phone",map.get("login_phone"));
            String timeToken = JwtUtil.createTimeToken(hashMap);
            boolean set = redisUtil.set(timeToken, map, -1 );
            boolean set1 = redisUtil.set(tokenKey, timeToken, -1 );
            HashMap<String, Object> result = new HashMap<>();
            result.put("token",timeToken);
            result.put("role",roles);
            return result;
        }
        return null;
    }

    @Transactional
    @Override
    public Boolean addAdminUser(LoginParams loginParams) {
        Login record = new Login();
        record.setLoginPassword(Md5Utils.md5(loginParams.getPassword()));
        record.setLoginPhone(loginParams.getUsername());
        record.setCreatetime(new Date());
        record.setLoginHeadimg("http://img0.imgtn.bdimg.com/it/u=3305298991,2024211813&fm=26&gp=0.jpg");
        int insert = loginMapper.insert(record);
        Boolean aBoolean = roleService.addUserRole(record.getLoginId(), 9);
        return aBoolean;
    }

    @Override
    public PagesBean<Map<String, Object>> adminList(Integer page, Integer size) {
        List<Map<String, Object>> maps = loginMapper.adminList((page - 1) * size, size);
        int i = loginMapper.adminListTotal();
        PagesBean<Map<String, Object>> mapPagesBean = new PagesBean<>();
        mapPagesBean.setData(maps);
        mapPagesBean.setDataCount(i);
        mapPagesBean.setNowsPage(page);
        mapPagesBean.setPageSize(size);
        return mapPagesBean;
    }

    @Override
    public boolean AdminstopAndStartUp(Integer loginId, Integer state) {
        return loginMapper.AdminstopAndStartUp(loginId,state)>0 ? true:false;
    }


}
